package com.xiaoxu.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class LogParser {

   static Logger logger = LoggerFactory.getLogger(LogParser.class);

    //20946835322
    // ^Ahttp://www.yihaodian.com/1/?tracker_u=2225501&type=3
    // ^Ahttp://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235
    // ^A1号店
    // ^A1
    // ^ASKAPHD3JZYH9EE9ACB1NGA9VDQHNJMX1NY9T
    // ^A^A^A^A
    // ^APPG4SWG71358HGRJGQHQQBXY9GF96CVU
    // ^A2225501^A\N
    // ^A124.79.172.232
    // ^A^Amsessionid:YR9H5YU7RZ8Y94EBJNZ2P5W8DT37Q9JH,unionKey:2225501
    // ^A^A2013-07-21 09:30:01
    // ^A\N
    // ^Ahttp://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235
    // ^A1^A^A\N^Anull^A-10^A^A^A^A
    // ^AMozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; MATP; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)
    // ^AWin32^A^A^A^A^A
    // ^A上海市
    // ^A1
    // ^A
    // ^A2013-07-21 09:30:01
    // ^A上海市
    // ^A
    // ^A66^A^A^A^A^A\N^A\N^A\N^A\N^A2013-07-21

    public Map<String, String> parse(String log) {

        Map<String, String> logInfo = new HashMap<>();
        IPParser ipParse = IPParser.getInstance();
        if (StringUtils.isNotBlank(log)) {
            String[] splits = log.split("\u0001");
            /**
             * index  info
             *  1  url
             *  10 sessionId
             *  13 ip
             *  17 time
             */
            String url = splits[1];
            String sessionId = splits[10];
            String ip = splits[13];
            String time = splits[17];

            logInfo.put("url", url);
            logInfo.put("sessionId", sessionId);
            logInfo.put("time", time);

            logInfo.put("ip", ip);

            IPParser.RegionInfo regionInfo = ipParse.analyseIp(ip);

            logInfo.put("country", regionInfo.getCountry());
            logInfo.put("province", regionInfo.getProvince());
            logInfo.put("city", regionInfo.getCity());
            return logInfo;

        } else {
            logger.error("日志格式不正确："+log);
        }

        return logInfo;


    }


    public static Map<String, String> parseAfterETL(String log) {

        Map<String, String> logInfo = new HashMap<>();
        if (StringUtils.isNotBlank(log)) {
            String[] splits = log.split("\t");

            //58.219.82.109
            // 中国
            // 江苏省
            // 无锡市
            // 3T489Y1W6TECTAKDFFTYMQUEH2EW5GG6
            // 2013-07-21 13:57:11
            // -
            // http://www.yihaodian.com/5/?tracker_u=2225501&type=4


            String province = splits[2];
            logInfo.put("province", province);


            String sessionId = splits[4];
            if (StringUtils.isNotBlank(sessionId)){
                logInfo.put("sessionId", sessionId);
            }else {
                logInfo.put("sessionId", "-");
            }

            return logInfo;

        } else {
            logger.error("日志格式不正确："+log);
        }

        return logInfo;


    }

}
